<template>

  <div>
    <!-- Breadcrumb 面包屑 导航区-->
    <!-- <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">头条管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>头条设置</el-breadcrumb-item>
    </el-breadcrumb> -->
    <!-- Card 卡片 卡片视图区-->
    <el-card class="box-card">
      <!-- 表单区-->
      <el-form ref="form" :model="form" :rules="rules">
        <!-- 图片1区-->
        <el-form-item label="图片1：">
          <el-row :gutter="10">
            <el-col :xs="8" :sm="6" :md="4" :lg="3" :xl="1">
              <el-input v-show="0" v-model="form.imgUrls1" />
              <img :src="form.imgUrls1" width="72px" height="45px">
            </el-col>
            <el-col :xs="8" :sm="6" :md="4" :lg="3" :xl="1">
              <el-button style="margin-left: 10px;" size="small" type="primary" @click="btclick(1)">选取图片</el-button>
            </el-col>
          </el-row>
          <!-- 图片2区-->
        </el-form-item>
        <el-form-item label="图片2：">
          <el-row :gutter="10">
            <el-col :xs="8" :sm="6" :md="4" :lg="3" :xl="1">
              <el-input v-show="0" v-model="form.imgUrls2" />
              <img :src="form.imgUrls2" width="72px" height="45px">
            </el-col>
            <el-col :xs="8" :sm="6" :md="4" :lg="3" :xl="1">
              <el-button style="margin-left: 10px;" size="small" type="primary" @click="btclick(2)">选取图片</el-button>
            </el-col>
          </el-row>
        </el-form-item>
        <!-- 图片3区-->
        <el-form-item label="图片3：">
          <el-row :gutter="10">
            <el-col :xs="8" :sm="6" :md="4" :lg="3" :xl="1">
              <el-input v-show="0" v-model="form.imgUrls3" />
              <img :src="form.imgUrls3" width="72px" height="45px">
            </el-col>
            <el-col :xs="8" :sm="6" :md="4" :lg="3" :xl="1">
              <el-button style="margin-left: 10px;" size="small" type="primary" @click="btclick(3)">选取图片</el-button>
            </el-col>
          </el-row>
        </el-form-item>
        <!--编辑器区-->
        <el-form-item>

          <quill-editor ref="myTextEditor" v-model="form.jianjieTxt" :options="editorOption" />

        </el-form-item>

        <el-form-item>
          <el-button type="primary" plain @click="saveEdit">修改</el-button>

        </el-form-item>
      </el-form>
    </el-card>
    <!--上传图片对话框-->
    <el-dialog
      title="图片上传"
      :visible.sync="DialogVisible"
      width="50%"
      :before-close="DialogClosed"
    >
      <el-upload
        ref="picUpload"
        :headers="headersObj"
        :action="uploadURL"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :on-success="handleSuccess"
        :multiple="false"
        :limit="1"
        list-type="picture"
      >
        <el-button size="small" type="primary">点击上传</el-button>

        <div slot="tip" class="el-upload__tip">只能上传pdf/jpg图片，且不超过10000kb</div>
      </el-upload>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelClick">取 消</el-button>
        <el-button type="primary" @click="saveClick">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
export default {
  components: {
    quillEditor
  },
  data() {
    return {
      DialogVisible: false, // 上传对话框显示与隐藏
      form: {
        id: '',
        imgUrls1: '',
        imgUrls2: '',
        imgUrls3: '',
        jianjie_txt: ''
      },
      editorOption: {// 编辑器设置
        placeholder: '请输入内容!'
      },
      rules: {
        jianjie_txt: [
          { required: true, message: '请输入内容！', trigger: 'blur' }
        ]
      },
      picnum: null, // 哪个图片
      picrul: '',
      uploadURL: 'http://csfg.heyishe.sc/admin/Uploader/pic_heng', // 上传组件的服务器地址
      preViewVisible: false, // 图片预览显示与隐藏
      previewPath: '', // 预览路径
      headersObj: {
        Authorization: window.sessionStorage.getItem('token')
      } // 上传组件的请求头部
    }
  },
  created() {
    this.getData()
  },

  methods: {
    // 优化异步操作 async await
    async getData() {
      const { data: res } = await this.$post('Headlines/detail', this.queryInfo)
      console.log(res)
      if (res.code !== 200) return this.$message.error('获取数据失败' + res.meta.msg)
      this.form = res.data.detail
      console.log(this.form)
    },
    btclick(item) { // 图片上传按钮点击
      this.DialogVisible = true
      this.picnum = item
    },
    DialogClosed() { // edit关闭窗口
      this.$refs.picUpload.clearFiles() // 清空上传文件
      this.DialogVisible = false
    },
    handleSuccess(response, file) {
      // 文件上传成功
      console.log('handleSuccess ', response)
      if (response.code !== 200) {
        return this.$message.error('上传文件失败' + response.meta.msg)
      }
      this.picrul = response.data.url
      this.$message.success('文件上传成功')
    },
    handleRemove(file, fileList) {
      // 文件上传删除
      console.log(file, fileList)
    },
    handlePreview(file) {
      // 文件上传预览
      console.log(file)
      this.previewPath = file.response.data.url
      this.preViewVisible = true
    },
    cancelClick() {
      // 取消点击
      if (this.picrul.length > 3) {
        this.$refs.picUpload.clearFiles() // 清空上传文件
      }
      this.DialogVisible = false
    },
    saveClick() {
      // 确定点击

      if (this.picrul.length > 3) {
        if (this.picnum === 1) {
          this.form.imgUrls1 = this.picrul
        }
        if (this.picnum === 2) {
          this.form.imgUrls2 = this.picrul
        }
        if (this.picnum === 3) {
          this.form.imgUrls3 = this.picrul
        }
        this.$refs.picUpload.clearFiles() // 清空上传文件
      }

      this.DialogVisible = false
    },
    saveEdit() {
      // 修改确定
      this.$refs.form.validate(async valid => {
        console.log(valid)
        if (!valid) {
          return false
        } // 验证失败
        // 发起添加用户表单的网络请求
        const { data: res } = await this.$post(
          'Headlines/edit', this.form
        )
        if (res.code !== 200) {
          return this.$message.error('修改失败！' + res.meta.msg)
        }
        this.$message.success('修改成功！')
        this.getData()
      })
    }

  }
}
</script>

    <style lang="less" scoped>
    </style>
